/**
 * @Title: TrashController.java
 * @Package me.springboot.mybatis.web
 * @Description: TODO
 * @author zengh
 * @date 2018/04/16
 * @version V1.0
 */
package me.springboot.mybatis.web;

import me.springboot.mybatis.common.utils.RoleType;
import me.springboot.mybatis.common.utils.SessionUtil;
import me.springboot.mybatis.core.RetResult;
import me.springboot.mybatis.core.RetResponse;
import me.springboot.mybatis.exception.ResultException;
import me.springboot.mybatis.model.Trash;
import me.springboot.mybatis.service.TrashService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import me.springboot.mybatis.web.req.DeviceReq;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zengh
 * @ClassName: TrashController
 * @Description: TODO
 * @date 2018/04/16
 */
@RestController
@RequestMapping("/trash")
@CrossOrigin
public class TrashController {

    @Resource
    private TrashService trashService;

    /**
     * @param trash
     * @Title: add
     * @Description: 添加
     * @Reutrn RetResult<Object>
     */
    @PostMapping("/add")
    public RetResult<Object> add(Trash trash) {
        trashService.save(trash);
        return RetResponse.makeOKRsp();
    }

    /**
     * @param trash
     * @Title: add2
     * @Description: 传入参数为JSON格式
     * @Reutrn RetResult<Object>
     */
    @PostMapping("/add2")
    public RetResult<Object> add2(@RequestBody Trash trash) {
        trashService.save(trash);
        return RetResponse.makeOKRsp();
    }

    /**
     * @Title: delete
     * @Description: 删除
     * @Reutrn RetResult<Object>
     */
    @PostMapping("/delete")
    public RetResult<Object> delete(@RequestParam Integer id) {
        trashService.deleteById(id);
        return RetResponse.makeOKRsp();
    }

    /**
     * @param trash
     * @Title: update
     * @Description: 更新
     * @Reutrn RetResult<Object>
     */
    @PostMapping("/update")
    public RetResult<Object> update(Trash trash) {
        trashService.update(trash);
        return RetResponse.makeOKRsp();
    }

    /**
     * @param trash
     * @Title: update
     * @Description: 更新(传入参数为JSON格式)
     * @Reutrn RetResult<Object>
     */
    @PostMapping("/update2")
    public RetResult<Object> update2(@RequestBody Trash trash) {
        trashService.update(trash);
        return RetResponse.makeOKRsp();
    }

    /**
     * @Title: detail
     * @Description: 根据ID查询详情
     * @Reutrn RetResult<Trash>
     */
    @PostMapping("/detail")
    public RetResult<Trash> detail(@RequestParam Integer id) {
        Trash trash = trashService.findById(id);
        return RetResponse.makeOKRsp(trash);
    }

    /**
     * @param reqMap
     * @Title: detail
     * @Description: 传入参数为JSON格式
     * @Reutrn RetResult<Trash>
     */
    @PostMapping("/detail2")
    public RetResult<Trash> detail(@RequestBody Map<String, Object> reqMap) {
        Integer id = Integer.valueOf(reqMap.get("id").toString());
        Trash trash = trashService.findById(id);
        return RetResponse.makeOKRsp(trash);
    }

    /**
     * @param page 页码
     * @param size 每页条数
     * @Title: list
     * @Description: 分页查询
     * @Reutrn RetResult<PageInfo<Trash>>
     */
    @PostMapping("/list")
    public RetResult<PageInfo<Trash>> list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<Trash> list = trashService.findAll();
        PageInfo<Trash> pageInfo = new PageInfo<Trash>(list);
        return RetResponse.makeOKRsp(pageInfo);
    }

    /**
     * @param page 页码
     * @param size 每页条数
     * @Title: search
     * @Description: 分页查询
     * @Reutrn RetResult<PageInfo<Trash>>
     */
    @PostMapping("/search")
    public RetResult<PageInfo<Map<String, Object>>> search(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size, DeviceReq deviceReq) {
        PageHelper.startPage(page, size);
        List<Map<String, Object>> list = trashService.search(deviceReq);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String, Object>>(list);
        return RetResponse.makeOKRsp(pageInfo);
    }

    @RequestMapping("/getDetail")
    public RetResult<PageInfo<Map<String, Object>>> getDetail(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "20") Integer size,
                                                              String device_type, String device_num,
                                                              String startTime, String endTime) {
        PageHelper.startPage(page, size);
        List<Map<String, Object>> list = trashService.getDetail(device_type, device_num, startTime, endTime);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String, Object>>(list);
        return RetResponse.makeOKRsp(pageInfo);
    }

    /**
     * 统计月异常数据
     *
     * @param request
     * @param device_type
     * @param device_num
     * @return
     */
    @RequestMapping("/countDataYiChang")
    public RetResult<List<Map<String, Object>>> countDataYiChang(HttpServletRequest request, String device_type, String device_num) {
        return RetResponse.makeOKRsp(trashService.countDataYiChang(device_type, device_num));
    }

    @RequestMapping("/getPosition")
    public RetResult<List<Map<String, Object>>> getPosition(HttpServletRequest request, @RequestParam(required = false) String devicenum, String device_type) throws ResultException {
        Map<String, String> param = new HashMap<>();
        if (RoleType.isKehu(request)) {
            param.put("kehu_userid", SessionUtil.getSessionUser(request).getuId().toString());
        } else if (RoleType.isXunJian(request)) {
            param.put("xunjian_userid", SessionUtil.getSessionUser(request).getuId().toString());
        }
        param.put("devicenum", devicenum);
        param.put("device_type", device_type);
        List<Map<String, Object>> list = trashService.getPosition(device_type, param);
        return RetResponse.makeOKRsp(list);
    }


    @RequestMapping("/getSingleDetail")
    public RetResult<Map<String, Object>> getSingleDetail(String device_type, String device_num) {
        Map<String, Object> resultMap = trashService.getSingleDetail(device_type, device_num);
        return RetResponse.makeOKRsp(resultMap);
    }

}